package br.com.covosys.controller;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.covosys.exception.ApplicationException;
import br.com.covosys.jsf.JSFKeys;
import br.com.covosys.model.ModelException;
import br.com.covosys.model.UsuarioModel;
import br.com.covosys.model.bean.TipoDeUsuario;
import br.com.covosys.model.bean.Usuario;
import br.com.covosys.util.JSFUtils;

@Scope("session")
@Controller
public class UsuarioController extends GenericController<Usuario, UsuarioModel> {
	
	private static final long serialVersionUID = 1L;
	
	private String confirmaSenha;
	
	@Autowired
	public UsuarioController(UsuarioModel usuarioModel) {
		super(usuarioModel);
		setBean(new Usuario());
		getBean().setTipoDeUsuario(new TipoDeUsuario());
		setFilter(new HashMap<String, Object>());
		setList(new ArrayList<Usuario>());
	}
	
	@Override
	public void cleanBean() throws ModelException, ApplicationException {
		super.cleanBean();
		getBean().setTipoDeUsuario(new TipoDeUsuario());
		this.confirmaSenha = null;
	}
	
	@Override
	public String save() throws ApplicationException, ModelException {
		try {
			getModel().save(bean, confirmaSenha);
			clean();
			return JSFKeys.LIST;
		} catch (ModelException modelException) {
			JSFUtils.addMessage(modelException.getMessage());
			return JSFKeys.INPUT;
		}
	}

	public String getConfirmaSenha() {
		return confirmaSenha;
	}

	public void setConfirmaSenha(String confirmaSenha) {
		this.confirmaSenha = confirmaSenha;
	}
}